package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.viewpager.widget.ViewPager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.TabViewPagerAdapter;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PagerItemVO;
import com.digitalchina.lxnsy.entity.PestDetail2VO;
import com.digitalchina.lxnsy.entity.PestListVO;
import com.digitalchina.lxnsy.fragment.ContentFragment;
import com.digitalchina.lxnsy.fragment.ExperienceFragment;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.view.MyTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/4/17 13:52
 * 修改备注：
 * ================================================
 */
public class PestDetailActivity extends BaseActivity implements BGABanner.Delegate<ImageView, String>, BGABanner.Adapter<ImageView, String> {

    @Bind(R.id.banner)
    BGABanner banner;
    @Bind(R.id.tab)
    MyTabLayout tab;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    private String id = "";
    private String name = "";

    /*每个 tab 的 item*/
    private List<PagerItemVO> mList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_pest_detail);

        PestListVO vo = (PestListVO) getIntent().getSerializableExtra("PestListVO");

        id = vo.getId();
        name = vo.getDiseasePestName();
        setTitle(name);

        setRightText("我的经验");
    }

    @Override
    public void initMember() {
        BusinessAgr.getPestDetail(this, id, mHandler);

        BusinessAccount.appBrowse(this, id, "15", name, "", mHandler);
    }

    @Override
    public void addListener() {
        banner.setDelegate(this);
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG.MSG_GET_PEST_DETAIL_SUCCESS:
                        callBack(message.obj);
                        break;
                    case MSG.MSG_GET_PEST_DETAIL_FIELD:

                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

        PestDetail2VO vo = FastJsonUtil.getBean(object.toString(), PestDetail2VO.class);

        if (vo == null)
            return;

        ContentFragment new1 = new ContentFragment();
        Bundle b1 = new Bundle();
        b1.putString("content", vo.getDiseasePestLaw());
        new1.setArguments(b1);

        ContentFragment new2 = new ContentFragment();
        Bundle b2 = new Bundle();
        b2.putString("content", vo.getPreventMethod());
        new2.setArguments(b2);

        ExperienceFragment new3 = new ExperienceFragment();
        Bundle b3 = new Bundle();
        b3.putString("disPestsWeedsId", id);
        new3.setArguments(b3);


        mList.add(new PagerItemVO("病症规律", new1));
        mList.add(new PagerItemVO("防治方法", new2));
        mList.add(new PagerItemVO("经验交流", new3));

        viewpager.setAdapter(new TabViewPagerAdapter(getFragmentManager(), mList));
        viewpager.setOffscreenPageLimit(3);
        tab.setViewPager(viewpager);



        if(vo.getPictureList()!=null){
            List<String> imagesList = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (int i = 0; i < vo.getPictureList().size(); i++) {
                imagesList.add(JointImageUrl.middleImageUrl(vo.getPictureList().get(i).getPictureVideoId()));
                titles.add("");
            }

            banner.setAdapter(this);
            banner.setAutoPlayInterval(5000);
            banner.setData(imagesList, titles);
        }

    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoActivity(this,MyExperienceListActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {

    }


    @Override
    public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
        Glide.with(itemView.getContext())
                .load(model)
                .placeholder(R.drawable.bg_default)
                .error(R.drawable.bg_default)
                .dontAnimate()
                .centerCrop()
                .into(itemView);
    }
}
